package com.zn.utils
{

    /**
     *格式化日期
     * @author zn
     *
     */
    public class DateFormatter
    {
        public function DateFormatter()
        {
        }

        /**
         *格式化 年月日
         * @param date
         * @return
         *
         */
        public static function formatterYMD(date:Date):String
        {
            var str:String;
            str = date.getFullYear() + "年" +
                Number(date.getMonth() + 1) + "月" +
                date.getDate() + "日";

            return str;
        }

        /**
         *格式化 时：分：秒
         * @param time
         * @return
         *
         */
        public static function formatterTime(time:Number, showAll:Boolean = false):String
        {
            var hours:int = int(time / 3600);
            var minutes:int = int(int(time % 3600) / 60);
            var seconds:int = int(time % 60);

            var str:String = "";
            if (showAll)
            {
                str = hours + "时";

                if (minutes < 10 && hours != 0)
                    str += "0" + minutes + "分";
                else
                    str += minutes + "分";

                if (seconds < 10 && minutes != 0)
                    str += "0" + seconds + "秒";
                else
                    str += seconds + "秒";
            }
            else
            {
                if (hours != 0)
                    str += hours + "时";
                if (minutes != 0)
                {
                    if (minutes < 10 && hours != 0)
                        str += "0" + minutes + "分";
                    else
                        str += minutes + "分";
                }
                if (seconds != 0)
                {
                    if (seconds < 10 && minutes != 0)
                        str += "0" + seconds + "秒";
                    else
                        str += seconds + "秒";
                }
            }

            return str;
        }
		
		/**
		 *格式化 年-月-日  时：分：秒：毫秒
		 * @param time
		 * @return 
		 * 
		 */
		public static function formatterTimeAll(time:Number):String
		{
			var date:String = "";
			var d:Date = new Date(time);
			
			date = d.getFullYear() + "-" +
				Number(d.getMonth() + 1) + "-" +
				d.getDate() + "  ";
			
			date += padTime(d.getHours()) + ":" +
				padTime(d.getMinutes()) + ":" +
				padTime(d.getSeconds()) + "." +
				padTime(d.getMilliseconds(), true)+ "  ";
			
			return date;
		}
			
		private static function padTime(num:Number, millis:Boolean = false):String
		{
			if (millis)
			{
				if (num < 10)
					return "00" + num.toString();
				else if (num < 100)
					return "0" + num.toString();
				else
					return num.toString();
			}
			else
			{
				return num > 9 ? num.toString() : "0" + num.toString();
			}
		}
    }
}